define(function(require, exports, module){

	var $ = require('jquery');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('jquery/jquery-cookie/1.3.1/jquery.cookie.js');
	require('jbox');
	
	var url = $CONFIG.base_url + '/api/configure/get';
	$.get(url,function(data){
		var login_photo_url = data.loginUrl;
		$(".login-left").css("background-image","url("+login_photo_url+")");
	});
	$(".ok-img").attr({style:'display:none;'});
	//正则表达式匹配函数test
	exports.match=function(o,reg,tip){
		//console.log($(o).val());
		var patten = new RegExp(reg);
		//console.log(patten.test($(o).val()));
		if(patten.test($(o).val())){
			$(tip).attr({style:'display:block;'});
		}else{
			$(tip).attr({style:'display:none;'});
		};
	};
	
	//为输入框注册事件
	exports.checkMail=function(o,reg,tip){
		$(o).keyup(function(){exports.match(o,reg,tip);});
		$(o).bind("paste",function(){exports.match(o,reg,tip);});
		$(o).blur(function(){exports.match(o,reg,tip);});		
	};
	exports.match("#username",/^[A-Za-z0-9][\\w\\-\\.]+@([\\w\\-]+\\.)+[\\w]{2,3}$/,"#username-ok");
	exports.match("#password",/^\w{6,18}$/,"#password-ok");
	
	$("#login-form").validate({
		rules : {
			username : {
				required :true,	
			},
			captcha : {
				required :true,
			}
		},
	errorElement: "span",
	errorLabelContainer : "#tip",
	messages : {
		username : {
			required:' 请输入邮箱',
			remote : ' 邮箱不存在',
		},
		captcha : {
			required : ' 请输入验证码',
			remote: ' 验证码错误',
		}
	},
	});
	$("#forget-form").validate({
		rules : {
			loginName : {
				required : true,
				remote: "forget/check_login_name"
			},
			captcha : {
				required : true,
				remote: "forget/check_captcha"
			}
		},
		messages : {
			loginName : {
				required:'请输入邮箱',
				remote : '邮箱不存在'
			},
			captcha : {
				required : '请输入验证码',
				remote:'验证码错误'
				
			}
		},
		errorLabelContainer : "#error"
	});
	
	$('.img_captcha_login').on('click', function(){$("#img_captcha_login").attr("src",$CONFIG.base_url+"/servlet/captchaCode?t=" + Math.random());});
	$('.img_captcha_forget').on('click', function(){$("#img_captcha_forget").attr("src",$CONFIG.base_url+"/servlet/captchaCode?t=" + Math.random());});
	$("#resend").click(function(e) {
		var date = form2js('login-form', '.', true);
		//////console.log(date);
		$.ajax({
			url : $CONFIG.base_url + "/login/send/activating/mail",
			type : "POST",
			data : date,
			contentType : "application/x-www-form-urlencoded",
			success : function() {
				t = "3";
				$("#profileNotice").show();
				time = setInterval(exports.time,1000);
				$.jBox.tip("邮件已经发送!");
			},
			error : function() {
				t = "3";
				$("#profileNotice").empty().append("<span class='show_error'></span>发送失败");
				$("#profileNotice").show();
				time = setInterval(exports.time, 1000);
			}
		});
	});
	
	exports.checkMail("#username",/^[A-Za-z0-9][\\w\\-\\.]+@([\\w\\-]+\\.)+[\\w]{2,3}$/,"#username-ok");
	
	exports.checkMail("#password",/^\w{6,18}$/,"#password-ok");
	
	// 定时器
	var t = 3;
	exports.time = function time() {
		t = t - 1;
		if (t < 0) {
			return false;
		}
		if (t == 0) {
			clearInterval(time);
			$(".letter_send_seccuss").hide();
		}
	};
	
	$(".login").click(function(){
		if($(".checkbox-login").prop("checked")){
			var json = $.toJSON(form2js("login-form", '.', true));
			//var postData = $("#login-form").serialize();
			//新建cookies,30天后过期
			$.cookie("cname", json,{ path: '/', expires: 30 });
		}
	});
	
	$(document).ready(function(){
		var cJsons=$.parseJSON($.cookie('cname'));
		
		$("#username").val(cJsons.username);
		$("#password").val(cJsons.password);
	});
});